library(dplyr)
library(readr)
library(maps)
library(ggplot2)
library(countrycode)
library(ggpattern)

# import data 
fulldat <- read_csv('Updated_ACP_Dataset_w_Covariates.csv')

# standardize country names and assign regions
fulldat$comname <- countryname(fulldat$country, destination = "cldr.short.en")
fulldat <- fulldat %>% 
  mutate(comname = case_when(
    country == "Turkiye" ~ "Turkey",
    TRUE ~ comname
  ))
fulldat$Region <- countryname(fulldat$comname, destination = "region")

# aggregate and categorize anti-China protests by country
countrysum <- fulldat %>% 
  group_by(comname) %>% 
  summarize(sum = sum(eventcount, na.rm = T)) %>% 
  mutate(sum_cat = case_when(
    sum < 20 ~ "less than 20",
    sum >= 20 & sum < 60 ~ "20 - 60",
    sum >= 60 & sum <= 300 ~ "60 - 300",
    sum > 300 ~ "more than 300"
  )) %>% 
  distinct(comname, .keep_all = TRUE)

# shift coordinates to recenter the world map
worldmap <- map_data("world", wrap = c(-25, 335))
worldmap$comname <- countryname(worldmap$region, destination = "cldr.short.en")

# fill countries with no record as missing
countrysum = rbind(countrysum, data.frame(comname=unique(worldmap$comname[!worldmap$comname %in% countrysum$comname]),
                                          sum = NA,
                                          sum_cat="no record")) %>% 
  mutate(sum_cat = factor(sum_cat, levels = c("less than 20", "20 - 60", "60 - 300", "more than 300", "no record"))) %>% 
  mutate(sum_cat = replace(sum_cat, comname == "China", NA))


# join protest data with map data
countrysum <- left_join(countrysum, worldmap, by = "comname") %>% distinct(comname, .keep_all = T)

# plot and save Figure 1
pdf(file = "ACP World Heat.pdf", width = 8, height = 5)
ggplot(countrysum) +
  geom_map(
    dat = worldmap, map = worldmap, aes(map_id = region),
    fill = "#f5f4f4", color = "white", size = 0.25
  ) +
  geom_map(map = worldmap, aes(map_id = region, fill = sum_cat), size = 0.25) +
  scale_fill_manual(name = "Number of Anti-China Protests 2000-2019",
                      values = c("#b1aeac", "#827d7a", "#4f4d4b", "#1d1c1b", "#e9e8e7"),
                    na.translate = F)+
  expand_limits(x = worldmap$long, y = c(-50, 90)) + 
  annotate("text", x=104, y=35, label="China", color = "#202020")+
  theme_void()+
  theme(axis.title = element_blank(),
        axis.text=element_blank(),
        axis.ticks=element_blank(),
        legend.position="bottom")
dev.off()

# aggregate anti-China protests by region
trend <- fulldat %>% 
  mutate(Region = factor(Region, levels = c("East Asia & Pacific", "Europe & Central Asia", "South Asia",
                                            "North America", "Latin America & Caribbean", "Middle East & North Africa",
                                            "Sub-Saharan Africa"))) %>% 
  group_by(Region, year) %>% 
  summarize(sum = sum(eventcount, na.rm = T))

# plot and save Figure 2
pdf(file = "ACP by Region.pdf", width = 8, height = 5)
trend %>% 
  ggplot(aes(x=year, y=sum, fill=Region)) +
  geom_area_pattern(
    aes(
      pattern = Region, 
      pattern_angle = Region
    ), 
    #fill            = Region, 
    colour          = 'black',
    pattern_spacing = 0.015
  )+
  scale_fill_grey(start = 0, end = 1) +
  labs(y= "Number of Anti-China Protests by Region and Year", x = NULL)+
  scale_x_continuous(breaks = seq(2000, 2019, by = 2))+
  scale_y_continuous(breaks = seq(0, 2800, by = 500))+
  theme_bw()
dev.off()

# plot and save Figure 4
pdf(file = "Myanmar.pdf", width = 6, height = 5, onefile=FALSE)
ggplot(fulldat %>% filter(country=="Myanmar"), aes(x=year, y=eventcount/cofdi_stock_gdp))+
  geom_point(size=2)+
  theme_classic()+
  scale_x_continuous(limits=c(2000, 2020), breaks = c(2000, 2004, 2008, 2012, 2016, 2020)) +
  labs(x="Year", y="Anti-China Protest Count / Chinese FDI stock as a share of GDP")+
  geom_vline(xintercept = 2010, linetype="dotted", size = 1, color="red")+
  geom_text(aes(x=2015.5, label="Myanmar General Election in 2010", y=10), colour="red")
dev.off() 


